#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include "../include/meta.h"


int hash(char *filename)
{
	char first_char;
	first_char = filename[0];

	int ascii_val = first_char;
	
	if(ascii_val>=48 && ascii_val<=57)
	{
		return (ascii_val+2)%50;

	}

	if(ascii_val>= 97 && ascii_val<=122)
	{
		return ((ascii_val+3)%100)+10;
	}

	if(ascii_val>=65 && ascii_val<=90)
	{
		return ((ascii_val+5)%70)+36;
	}
	else 
	return 62;
}

void hash_display()
{
	printf("in display\n");
	int i=0;
	struct list *temp;
	for(i=1;i<63;i++)
	{
		while(hash_table[i]!=NULL)
		{	
			temp = hash_table[i];
			while(temp!=NULL)
			{	
				printf("\n loc is : %d\t",i);
				printf("\n %s\t", temp->filename);
				printf("\n %s\t \n", temp->pathname);
				temp=temp->next;
			}
			
			i++;
			if(i==63)
				break;
		}
	}
}


struct list* hash_search(int index, char name_File[])
{	
      struct list *start,*fresh, *fresh_head,*fresh_temp;
	
	fresh_head= (struct list *)malloc(sizeof(struct list));
	fresh_temp=fresh_head;
      start=hash_table[index];	
	while(start!=NULL)
	{	if (strcmp(start->filename,name_File)==0)
		{	fresh= (struct list *)malloc(sizeof(struct list));
			strcpy(fresh->filename,start->filename); 
			strcpy(fresh->pathname, start->pathname); 
			fresh->next=NULL;

			fresh_temp->next=fresh;
			fresh_temp=fresh_temp->next;
			                     
		}
		//printf("%s ",fresh->pathname);
		start = start->next;
	}

return fresh_head->next;

}



struct list* searchStartPoint(char *name_File)
{	int index;
         index = hash(name_File);
 	struct list* temp= (struct list *)malloc(sizeof(struct list));
	if(hash_table[index] == NULL);
		//printf("0 Files Found. Search Ended...\n");	
	else if(index == -1);
		//printf("Invalid file name\n");
	else
		{temp= hash_search(index,name_File); return temp; }
return NULL;
}



void insert_into_list(int hashvalue, char file[],char path[])
{
	struct list *node;
	node = (struct list *)malloc(sizeof(struct list));

	strcpy(node->filename, file);
	strcpy(node->pathname, path);
	node->next = NULL;

	if(hash_table[hashvalue]== NULL)
		hash_table[hashvalue]= node;

	else
	{
		struct list *temp;
		temp = hash_table[hashvalue];

		while(temp->next!=NULL)
			temp = temp->next;

		temp->next = node;	
	}
	//printf("\n hash insertion success\n ");
} 





int hash_deletion(int hashvalue, char *f,char *p)
{
	struct list * temp1, *temp2,*del;
	temp1 = hash_table[hashvalue];
	temp2 = hash_table[hashvalue];
	
	if(temp1==NULL)
                {
                  //printf("no entry to delete");
                  return 0;
                }

	if(strcmp(temp1->filename,f)==0 && strcmp(temp1->pathname,p)==0)
	{
           
              
		temp2= temp1->next;
		hash_table[hashvalue]=temp2;
		free(temp1);
		//printf("%s is deleted",f,p);
		return 1;
	}
	else
	{
		while(temp1!= NULL)
		{
	
			if(strcmp(temp1->filename,f)==0 && strcmp(temp1->pathname,p)==0)
			{       
				{       
					temp2->next=temp1->next;
					del=temp1;
                                        temp1=temp1->next;
					free(del);
					
				}
			//printf("\n %s is deleted from \t %s",f,p);
			}
			
			else
			{
				temp2 = temp1;					
				temp1 = temp1->next;
				
			}
		} 
	}
	//printf("\n hash deletion success \n");
	return 1;
}

/*struct list* substring_search(char *start_filesname)
{       int index,length;
        struct list *start,*fresh,*temp,*head;
        head=(struct list *)malloc(sizeof(struct list));
        temp=head;
        index = hash(start_filesname);
        start=hash_table[index];
        length=strlen(start_filesname);
        if(hash_table[index] == NULL)
        {
                return NULL;
        }
        else if(index == -1)
        {
                return NULL;
        }
        else{
                        while(start!=NULL)
                {      
                        if (strncmp(start->filename,start_filesname,length)==0)
                           {    
                                fresh= (struct list *)malloc(sizeof(struct list));
                                strcpy(fresh->pathname,start->pathname);  
                                strcpy(fresh->filename,start->filename);                
                                fresh->next=NULL;
                                temp->next=fresh;
                                temp=fresh;              
                           }
                        start = start->next;
                }
               
        }
          return head->next;
}
*/


